package com.study.chapter05.operator.transform;

import com.study.entity.Event;
import com.study.chapter05.source.ClickSource;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Description:
 * @Author: LiuQun
 * @Date: 2022/7/29 21:03
 */
public class ReduceOperatorTest {
    public static void main(String[] args) throws Exception {
        //环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //使用自定义的SourceFunction，注意对于自定义的直接继承SourceFunction的，并行度只能设置为1，否则会抛出异常
        DataStreamSource<Event> stream = env.addSource(new ClickSource()).setParallelism(1);

        stream.print("stream：");

        //1.统计每个用户的访问频次
        SingleOutputStreamOperator<Tuple2<String, Long>> clickByUser = stream
                .map(data -> Tuple2.of(data.getUser(), 1L)) //将数据封装成这种形式(用户名,1L)
                .returns(Types.TUPLE(Types.STRING, Types.LONG))
                .keyBy(data -> data.f0) //将数据根据用户名进行分组
                .reduce(new ReduceFunction<Tuple2<String, Long>>() {
                    @Override
                    public Tuple2<String, Long> reduce(Tuple2<String, Long> value1, Tuple2<String, Long> value2) throws Exception {
                        //将同名称的用户访问次数相加
                        return Tuple2.of(value1.f0, value1.f1 + value2.f1);
                    }
                });

        //2.选取最活跃的用户
        SingleOutputStreamOperator<Tuple2<String, Long>> result = clickByUser
                .keyBy(data -> "sameKey") //所有数据分到同一组，因为要进行聚合操作必须先进行keyBy分组
                .reduce(new ReduceFunction<Tuple2<String, Long>>() {
                    @Override
                    public Tuple2<String, Long> reduce(Tuple2<String, Long> t1, Tuple2<String, Long> t2) throws Exception {
                        //获取当前活跃数最大的数据
                        return t1.f1 > t2.f1 ? t1 : t2;
                    }
                });

        result.print("result：");

        env.execute();
    }
}
